package org.epsi.blokus.salon;
import java.util.Observable;
import java.util.Observer;
import org.epsi.blokus.Hex;
import org.epsi.blokus.commun.Message;

/**
 * Création et gestion d'un salon
 * @author alex
 */
public class CreateSalon extends javax.swing.JFrame  implements Observer {
    
    private ServerClient client;
    private boolean salonCree=false;

    public CreateSalon(ServerClient client) {

        this.client = client;
        initComponents();

        // Les évènements sont aussi écoutés par cette fenêtre
        this.client.addObserver(this);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtNomSalon = new javax.swing.JTextField();
        BtnCreer = new javax.swing.JButton();
        BtnLancer = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtLogs = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        txtMaitre = new javax.swing.JTextField();
        txtEsclave = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnFermer = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Création d'un nouveau salon");
        setMinimumSize(new java.awt.Dimension(351, 220));

        jLabel1.setText("Nom du salon");

        BtnCreer.setText("Creer");
        BtnCreer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCreerActionPerformed(evt);
            }
        });

        BtnLancer.setText("Lancer");
        BtnLancer.setEnabled(false);
        BtnLancer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnLancerActionPerformed(evt);
            }
        });

        txtLogs.setColumns(20);
        txtLogs.setEditable(false);
        txtLogs.setRows(5);
        jScrollPane1.setViewportView(txtLogs);

        jLabel3.setText("Maitre");

        txtMaitre.setEditable(false);

        txtEsclave.setEditable(false);

        jLabel4.setText("Invité");

        btnFermer.setText("Fermer");
        btnFermer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFermerActionPerformed(evt);
            }
        });

        jLabel5.setText("Logs");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtNomSalon, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtMaitre, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(txtEsclave, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)))
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(BtnLancer, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                            .addComponent(btnFermer, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                            .addComponent(BtnCreer, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)))
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNomSalon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)))
                    .addComponent(BtnCreer, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(BtnLancer, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnFermer, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtEsclave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtMaitre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addGap(1, 1, 1)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Créer nouveau Salon
     * @param evt
     */
    private void BtnCreerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCreerActionPerformed
       Message m = new Message();
       m.setTypeMessage(Message.TTypeMessage.creation_salon).addObject("NOMSALON",this.txtNomSalon.getText());
       client.envoyer(m);
    }//GEN-LAST:event_BtnCreerActionPerformed

    /**
     * Fermer Salon
     * @param evt
     */
    private void btnFermerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFermerActionPerformed
       if (salonCree) {
           Message m = new Message();
           m.setTypeMessage(Message.TTypeMessage.fermeture_salon);
           client.envoyer(m);
       } else {
           this.dispose();
       }
      
    }//GEN-LAST:event_btnFermerActionPerformed
    
    /**
     * Lancer la partie
     * @param evt
     */
    private void BtnLancerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnLancerActionPerformed
        client.envoyer(new Message(Message.TTypeMessage.lancer_partie));
        Hex.getInstance().newOnlineGame(this.client, true);
    }//GEN-LAST:event_BtnLancerActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnCreer;
    private javax.swing.JButton BtnLancer;
    private javax.swing.JButton btnFermer;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtEsclave;
    private javax.swing.JTextArea txtLogs;
    private javax.swing.JTextField txtMaitre;
    private javax.swing.JTextField txtNomSalon;
    // End of variables declaration//GEN-END:variables

    /**
     * Ecoute des message envoyés par le serveur
     * @param o
     * @param arg
     */
    public void update(Observable o, Object arg) {
        Message mess = (Message)arg;
        switch(mess.getTypeMessage())
        {
            // Ouverture du salon
            case ouverture_salon:
                this.BtnCreer.setEnabled(false);
            break;

            // Création du salon réussie
            case creation_salon_ack:
                String nomSalon = (String)mess.getObject("NOMSALON");
                String nomMaitre = (String)mess.getObject("MAITRE");
                this.addLineLogs(nomSalon);
                this.setLineMaitre(nomMaitre);
                this.salonCree = true;
            break;

            // Entrée d'un invité
            case entrer_salon_ack:
                 String invite = (String)mess.getObject("INVITE");
                 this.txtLogs.setText(invite+ " a rejoint la parite");
                 this.setLineEsclave(invite);
                 this.BtnLancer.setEnabled(true);
            break;

            // Sortie de l'invité
            case sortir_salon_ack:
                 String inviteSortie = (String)mess.getObject("INVITE");
                 this.txtLogs.setText(inviteSortie+ " quitte la parite");
                 this.setLineEsclave("");
                 this.BtnLancer.setEnabled(false);
            break;

            // Fermeture du salon confirmée : on ferme la fenêtre
            case fermeture_salon_ack:
                this.dispose();
            break;

        }
    }



    // ------------ Mise à jour de la GUI --------------------- //
    
    public void addLineLogs (String txt){
        String message = "Le salon " + txt + " à bien été créé";
        this.txtLogs.setText(this.txtLogs.getText()+message);
    }

    public void setLineMaitre (String txt){
        this.txtMaitre.setText(txt);
    }

    private void setLineEsclave(String txt) {
        this.txtEsclave.setText(txt);
    }

}
